package com.example.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.operation.domin.Coupons;
import com.example.operation.dto.CouponsDto;
import com.example.operation.mapper.CouponsMapper;
import com.example.operation.service.CouponsService;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName: CouponsServiceImpl
 * @Description:
 * @Author
 * @Date 2021/11/26
 * @Version 1.0
 */

@Service
public class CouponsServiceImpl extends ServiceImpl<CouponsMapper, Coupons> implements CouponsService {

    @Autowired
    private CouponsMapper couponsMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(CouponsDto couponsDto) {
        Coupons coupons = new Coupons();
        LambdaQueryWrapper<Coupons> eq = new QueryWrapper<Coupons>()
                .lambda().eq(Coupons::getName,couponsDto.getName());
        Integer count = couponsMapper.selectCount(eq);
        if(count > 0){
            throw new RuntimeException("优惠券名称已存在，请重新输入优惠券名称");
        }
        BeanUtils.copyProperties(couponsDto,coupons);
        coupons.setStartTime(LocalDateTime.now());
        coupons.setCreateTime(LocalDateTime.now());
        coupons.setEndTime(LocalDateTime.now());
        couponsMapper.insert(coupons);
    }

    @Override
    public Page<Coupons> page(Integer pageCurrent, Integer pageSize, Integer pageNumber, String name, Integer state, String couponsType) {
    /*    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));*/

        Page<Coupons> page = new Page<>(pageCurrent,pageSize,pageNumber);
        //eq等值查询，lt小于查询，le小于等于查询
        //gt大于查询，ge大于等于查询
        LambdaQueryWrapper<Coupons> eq = new QueryWrapper<Coupons>().lambda()
                .eq(StrUtil.isNotBlank(couponsType),Coupons::getCouponsType,couponsType)
                .like(StrUtil.isNotBlank(name),Coupons::getName,name);
        //已到期
        if(state.equals(2)){
            eq.lt(Coupons::getEndTime, LocalDateTime.now());
        }
        //未开始
        else if(state.equals(1)){
            eq.gt(Coupons::getStartTime, LocalDateTime.now());
        }
        //正在派券
        else if(state.equals(0)){
            eq.le(Coupons::getStartTime, LocalDateTime.now())
                    .gt(Coupons::getEndTime,LocalDateTime.now());
        }
        else {
            throw new RuntimeException("优惠券格式错误，请重新确认格式");
        }
        return couponsMapper.selectPage(page,eq);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delectById(Integer id) {
        couponsMapper.deleteById(id);
    }

    @Override
    public Coupons selectByType(String couponsType) {

        LambdaQueryWrapper<Coupons> eq = new QueryWrapper<Coupons>()
                .lambda().eq(Coupons::getCouponsType,couponsType);
        if(couponsType == null) {
           throw new RuntimeException("查询格式错误，请稍后重试");
        }else {
        return couponsMapper.selectOne(eq.last("limit 1"));}
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer update(CouponsDto couponsDto) {
        Coupons coupons = new Coupons();
        LambdaQueryWrapper<Coupons> eq = new QueryWrapper<Coupons>()
                .lambda().eq(Coupons::getName,couponsDto.getName());
        Integer count = couponsMapper.selectCount(eq);
        if(count == 0){
            throw new RuntimeException("当前优惠券名称不存在，请重新输入");
        }
        BeanUtils.copyProperties(couponsDto,coupons);
        coupons.setStartTime(LocalDateTime.now());
        coupons.setCreateTime(LocalDateTime.now());
        coupons.setEndTime(LocalDateTime.now());
        return couponsMapper.update(coupons,eq);
    }
}
